package net.zypro.sample;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import net.zypro.sample.model.Account;
import net.zypro.sample.model.ErrorMessage;
import net.zypro.sample.presenter.AccountPresenter;
import net.zypro.sample.util.LogUtils;
import net.zypro.sample.view.LoginView;


public class MainActivity extends Activity implements LoginView {
    private final static String TAG= LogUtils.makeTag(MainActivity.class);

    private EditText usernameEdtTxt;
    private EditText passwordEdtTxt;
    private Button loginBtn;
    private TextView uidTxt;
    private TextView nicknameTxt;
    private ProgressDialog mPgd;

    private AccountPresenter authPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        usernameEdtTxt=(EditText)findViewById(R.id.edtTxt_username);
        passwordEdtTxt=(EditText)findViewById(R.id.edtTxt_password);
        loginBtn=(Button)findViewById(R.id.btn_login);
        uidTxt=(TextView)findViewById(R.id.txt_uid);
        nicknameTxt=(TextView)findViewById(R.id.txt_nickname);

        mPgd=new ProgressDialog(this);

        authPresenter=new AccountPresenter(this,this);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username=usernameEdtTxt.getText().toString();
                String password=passwordEdtTxt.getText().toString();

                //验证字段有效性

                authPresenter.login(username,password);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void toast(String str)
    {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startLogin() {
        mPgd.setCancelable(false);
        mPgd.show();
    }

    @Override
    public void endLogin() {
        mPgd.dismiss();
    }

    @Override
    public void setAuthResult(Account account) {
        if(account!=null)
        {
           toast("登录成功");
           uidTxt.setText(account.getUid());
           nicknameTxt.setText(account.getNickname());
        }
    }

    @Override
    public void onError(ErrorMessage msg) {
        switch (msg.getCode())
        {
            case C.ErrorCode.USER_AUTH_FAILED_ERROR:
                 toast("登录失败,用户名或密码错误");
                 break;
            default:
                 toast(msg.getMessage());
        }
    }
}
